package com.eipc;

import org.androidpn.sdk.Constants;
import org.androidpn.sdk.XmppManager;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.eipc.model.Response;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.JsonParse;
import com.eipc.util.MyLog;

/**
 * 咨询服务中心
 * @author x_liaolijun
 *
 * 2010-9-24 下午12:05:57
 *  
 * ConsultActivity
 *
 */
public class ConsultActivity extends CommonActivity {
	private ListView mContent;
	private Cursor mCursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.consult);
		initView();
	}
	
	@Override
	protected void updateView() {
		super.updateView();
		mCursor = mDB.queryAll(DBHelper.TB_CONSULT);
		mContent.setAdapter(new ContentAdapter(mCursor));
	}
	
	protected void initView(){
		super.initView();
		mBar.setOnRightClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new PollingConsultTask().execute();
			}
		});
		mContent = (ListView)findViewById(R.id.list1);
		mCursor = mDB.queryAll(DBHelper.TB_CONSULT);
		mBar.setMiddleText(R.string.consult_service_center);
		mContent.setAdapter(new ContentAdapter(mCursor));
		mContent.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showDailog((Cursor)mContent.getItemAtPosition(position));
			}
		});
	}
	
	/*
	 * 创建对话框
	 * 
	 * @param info
	 */
	private void showDailog(final Cursor info) {
		if (info != null) {
			final Dialog dialog = new Dialog(this);
			dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			View view = getLayoutInflater().inflate(
					R.layout.consult_dialog, null);
			TextView tv = (TextView)view.findViewById(R.id.content);
			tv.setText(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_CONSULT_CONTENT)));
			tv = (TextView)view.findViewById(R.id.tv_time);
			tv.setText(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_CONSULT_SEND_TIME)));
			Button btn = (Button)view.findViewById(R.id.btn_close);
			btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.setContentView(view);
			dialog.show();
		}
	}
	
	class ContentAdapter extends CursorAdapter{
		public ContentAdapter(Cursor cursor){
			super(ConsultActivity.this, cursor);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = getLayoutInflater().inflate(
					R.layout.consult_item, null);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView tv = (TextView)view.findViewById(R.id.text1);
			tv.setText(getTitle(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_CONSULT_CONTENT))));
			tv = (TextView)view.findViewById(R.id.text2);
			tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_CONSULT_SEND_TIME)));
		}	
	}
	
	/*
	 * 截取第一个冒号之前的作为标题
	 */
	private String getTitle(String content){
		if(content!=null){
			int index = content.indexOf("：");
			return index!=-1?(content.substring(0, index)):content;
		}
		return content;
	}
	
	class PollingConsultTask extends CommonTask<Void>{
		public PollingConsultTask(){
			super(ConsultActivity.this);
		}
		private String mResult;
		@Override
		protected Void doInBackground(Void... params) {
			Log.i("--"+this.getClass().getName(),"------------------polling Consult------------------ t : ");
			try {
				SharedPreferences sdkPreferences = getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
				String mobileID = sdkPreferences.getString(Constants.XMPP_USERNAME,"");
				String maxID = "-2";
				String ids = mDB.getConsultIDs();
				mResult = HttpManager.polling(getString(R.string.url_polling),Constants.TTYPE_CONSULT,maxID,ids,mobileID,"0");
			} catch (Exception e) {
				MyLog.error(FetchConsultService.class,"",e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Response res;
			try{res = JsonParse.parseRespnse(mResult);}catch(Exception e){res = null;}
            if(XmppManager.isLogin&&res!=null&&res.success){
            	//TODO 刷新成功 等待 PUSH	
			}else{
				mBuilder.setMessage(R.string.status_connect_failed).setNegativeButton(R.string.btn_close, null).show();
				Log.e(this.getClass().getName()+" polling failure : ","mResult is null");
			}
		}
		
	}
}
